window.onload = function () {
  const urlParams = new URLSearchParams(window.location.search);
  const name = urlParams.get('name');
  const status = urlParams.get('status');
  const comment = urlParams.get('comment');
  console.log(name)
  console.log(status)
  const commentInput = document.getElementById('commentInput');
  if (commentInput) {
    commentInput.value = decodeURIComponent(comment);
  }
};
document.addEventListener('DOMContentLoaded', function () {
  const resumeData = JSON.parse(localStorage.getItem('resumeData'));
  const resumeId = resumeData ? resumeData.id : null;
  const deleteCommentBtn = document.getElementById('deleteCommentBtn');
  if (deleteCommentBtn) {
    deleteCommentBtn.addEventListener('click', function () {
      if (!resumeId) {
        alert('未获取到简历ID，请重新查看简历');
        return;
      }
      fetch(`http://192.168.244.14:8080/comment/delete/${resumeId}`, {
        method: 'DELETE'
      })
        .then(response => {
          if (!response.ok) {
            throw new Error('网络响应错误：' + response.status);
          }
          return response.json();
        })
        .then(data => {
          console.log('后端返回的删除留言数据:', data);
          if (data && data.code === 1) {
            alert('留言删除成功');
            const commentInput = document.getElementById('commentInput');
            commentInput.value = '';
            window.location.href = "http://127.0.0.1:5501/sparkResume/html/checkResume.html";
          } else {
            alert('删除留言失败：' + (data && data.msg ? data.msg : '未知错误'));
          }
        })
        .catch(error => {
          console.error('删除留言请求出错:', error);
          alert('删除留言请求出错，请检查网络连接');
        });
    });
  }
});

document.addEventListener('DOMContentLoaded', function () {
  const editCommentBtn = document.getElementById('submitCommentBtn');
  if (editCommentBtn) {
    editCommentBtn.addEventListener('click', function () {
      const resumeData = JSON.parse(localStorage.getItem('resumeData'));
      const userId = resumeData ? resumeData.usersId : null;
      const resumeId = resumeData.id;
      if (!userId) {
        alert('未获取到用户 ID，请重新查看简历');
        return;
      }
      const commentInput = document.getElementById('commentInput');
      const comment = commentInput.value;
      const requestBody = {
        comment: comment
      };
      fetch(`http://192.168.244.14:8080/comment/update/${resumeId}/${userId}`, {
        method: 'PUT',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(requestBody)
      })
        .then(response => {
          if (!response.ok) {
            throw new Error('网络响应错误：' + response.status);
          }
          return response.json();
        })
        .then(data => {
          let statusText;
          switch (parseInt(resumeData.status)) {
            case 1:
              statusText = '已投递';
              break;
            case 2:
              statusText = '已查看';
              break;
            case 3:
              statusText = '已通过';
              break;
            case 4:
              statusText = '未通过';
              break;
            default:
              statusText = '未知状态';
          }
          alert('修改留言成功')
          const url = `http://127.0.0.1:5501/sparkResume/html/checkResume.html`;
          window.location.href = url;
        })
        .catch(error => {
          console.error('获取留言内容请求出错:', error);
          alert('获取留言内容请求出错，请检查网络连接');
        });
    });
  }
});